package de.bomberman.appstate;

import com.jme3.app.Application;
import com.jme3.renderer.ViewPort;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeSystem;
import com.jme3.system.Timer;
import de.bomberman.debug.Config;
import factory.AppSettingsFactory;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.logging.Logger;

public class MainApp extends Application {

    // the states
    private MenuState menuState = null;
    private GameState gameState = null;
    // thread pool
    public static ScheduledThreadPoolExecutor executor;
    // Logger
    private static final Logger logger = Logger.getLogger(MainApp.class.getName());

    public MainApp() {
        // log level
        Logger.getLogger("").setLevel(Config.LOGGER_LEVEL);

        executor = new ScheduledThreadPoolExecutor(10);
    }

    @Override
    public void start() {
        if (settings == null) {
            AppSettings mySettings = AppSettingsFactory.getAppSettings();
            setSettings(mySettings);
        }

        // show settings in final version
        if (Config.SHOW_SETTINGS_DIALOG) {
            JmeSystem.showSettingsDialog(settings, false);
        }
        super.start();
    }

    @Override
    public void initialize() {
        super.initialize();

        // create the states, the parameter is the application
        menuState = new MenuState();
        gameState = new GameState(this);

        if (Config.NO_MENU) {
            getStateManager().attach(gameState);
        } else {
            getStateManager().attach(menuState);
        }
    }

    @Override
    public void stop() {
        super.stop();
        executor.shutdown();
    }

    @Override
    public void update() {
        super.update();
        float tpf = timer.getTimePerFrame() * speed;

        // update states
        stateManager.update(tpf);
        // render states
        stateManager.render(renderManager);
        renderManager.render(tpf, true);

    }

    public void loadMenu() {
        getStateManager().detach(gameState);
        getStateManager().attach(menuState);

    }

    public void loadGame() {
        getStateManager().detach(menuState);
        getStateManager().attach(gameState);
    }

    public void loadLevelEditor() {
        getStateManager().detach(menuState);
    }

    @Override
    public ViewPort getViewPort() {
        return viewPort;
    }

    public ViewPort getGUIViewPort() {
        return guiViewPort;
    }

    @Override
    public Timer getTimer() {
        return timer;
    }

    public static void main(String[] args) {
        new MainApp().start();
    }
}
